// SPDX-License-Identifier: GPL-2.0 or GPL-3.0
// Copyright © 2019 Ariadne Devos
/* sHT -- find a byte in a string */

#include <limits.h>
#include <stddef.h>
#include <sHT/bitvec.h>
#include <sHT/index.h>
#include <sHT/lex.h>
#include <sHT/test.h>

size_t
sHT_lex_skip(const unsigned char from[], size_t n, const struct sHT_lex_type *c, void *x)
{
	/* TODO: word-at-a-time */
	/* This can be assigned to the return register */
	size_t i;
	/* TODO: variant which always performs one iteration
	   (less branching, shorter code) */
	sHT_index_iterate(i, n) {
		/* bounds: @var{sHT_index_iterate} */
		unsigned char b = from[i];
		if (sHT_bit_test(c->c_allow, b))
			continue;
		/* A syntax error or the terminator. */
		/* 1: terminator, 0: syntax error */
		int which = sHT_eq_bool(c->c_stop, b);
		/* In any case, pass the number of skipped/parsed bytes,
		  not the index of the last. */
		i++;
		return c->cb_skip_done[which](i, x);
	}
	/* ‘The detection may speculatively be incorrect.’
	  Any of @var{i} and @var{n} would do, but @var{i} produced
	  smaller code on x86-64 SystemV (153 < 155). */
	return i;
}
